package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.dto.MaterialReportVO;
import org.linlinjava.litemall.admin.service.AdminFormulaService;
import org.linlinjava.litemall.admin.service.AdminMaterialService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.Material;
import org.linlinjava.litemall.db.service.FormulaService;
import org.linlinjava.litemall.db.service.MaterialService;
import org.linlinjava.litemall.db.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/material")
@Validated
public class AdminMaterialController {
    private final Log logger = LogFactory.getLog(AdminMaterialController.class);

    @Autowired
    private MaterialService materialService;

    @Autowired
    private FormulaService formulaService;

    @Autowired
    private ProductService productService;

    @Autowired
    private AdminFormulaService adminFormulaService;

    @Autowired
    private AdminMaterialService adminMaterialService;


    @RequiresPermissions("admin:material:list")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String id, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<Material> brandList = materialService.querySelective(id, name, page, limit, sort, order);
        return ResponseUtil.okList(brandList);
    }

    private Object validate(Material brand) {
        String name = brand.getName();
        if (StringUtils.isEmpty(brand.getCode())) {
            return ResponseUtil.badArgument();
        }
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }

        String desc = brand.getUnit();
        if (StringUtils.isEmpty(desc)) {
            return ResponseUtil.badArgument();
        }

        BigDecimal price = brand.getPrice();
        if (price == null) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    @RequiresPermissions("admin:material:create")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody Material brand) {
        Object error = validate(brand);
        if (error != null) {
            return error;
        }
        materialService.add(brand);
        return ResponseUtil.ok(brand);
    }

    @RequiresPermissions("admin:material:read")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        Material brand = materialService.findById(id);
        return ResponseUtil.ok(brand);
    }

    @RequiresPermissions("admin:material:update")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody Material brand) {
        Object error = validate(brand);
        if (error != null) {
            return error;
        }
        if (materialService.updateById(brand) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(brand);
    }

    @RequiresPermissions("admin:material:delete")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody Material brand) {
        Integer id = brand.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        materialService.deleteById(id);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:material:search")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "搜索")
    @GetMapping("/search")
    public Object search(@RequestParam String keyword) {
        if (StringUtils.isEmpty(keyword)) {
            return ResponseUtil.badArgument();
        }
        List<Material> materials = materialService.search(keyword);
        return ResponseUtil.ok(materials);
    }

    @RequiresPermissions("admin:material:import")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "导入数据")
    @PostMapping("/import")
    public Object importData(MultipartFile file)   {
        adminFormulaService.excel2Employee(file);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:material:report")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "原料报表")
    @GetMapping("/report")
    public Object report(String name,
                       @RequestParam(required = false) Long startTime,
                       @RequestParam(required = false) Long endTime) {
        List<MaterialReportVO> brandList = adminMaterialService.querySelective(startTime,endTime, name);

        return ResponseUtil.okList(brandList);
    }


    @RequiresPermissions("admin:material:info")
    @RequiresPermissionsDesc(menu = {"物料管理", "原料管理"}, button = "首页信息")
    @GetMapping("/info")
    public Object info() {

        Map<String,Long> map=new HashMap<>();

        Long materialTotal = materialService.count();
        Long formulaTotal = formulaService.count();
        Long productTotal = productService.count();
        map.put("productTotal",productTotal);
        map.put("materialTotal",materialTotal);
        map.put("formulaTotal",formulaTotal);
        return ResponseUtil.ok(map);
    }

}
